<?php
/**
 * Object Relational Persistence Mapping Class for table: department
 *
 * @package	com.clearhealth
 * @author	ClearHealth Inc.
 */
class Department extends ORDataObject {
	var $department_id		= '';
	var $vi_name			= '';
	var $en_name			= '';
	var $vi_description		= '';
	var $en_description		= '';
	var $deleted 			= '';

	var $_table = 'department';
	var $_key = 'department_id';
	var $_internalName = 'Department';

	const EXAMINATION_DEPT_ID  =   1;
	const EMERGENCY_DEPT_ID    =   5;
	const LABORATORY_DEPT_ID   =   6;
	const RADIOLOGY_DEPT_ID    =   7;
	const ODONTOLOGY_DEPT_ID   =   8;
	
	function Department() {
		parent::ORDataObject();
	}
	
	public function listAll() {
		$field	=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field,"name");
		$ds = new Datasource_sql();
		$sql = array (
			'cols' 		=> 	"$this->_key, $field as name",
			'from'		=> 	"$this->_table",
			'where'		=> "deleted = 0",
			'orderby'	=>	"$this->_key");
		$ds->setup($this->_db, $sql);
		return $ds->toArray("$this->_key", 'name');
	}
		/**
	 * @author hdviet
	 * update for localize others
	 */
	function addDepartment($department){
	    $locales = L10nHelper::getSupportedLocales();
		$data = array();
       	foreach ( $locales as $lo => $la ) {
       		//save name
       		$nameLo	=	L10nHelper::changeField($lo, "name");
       		$data[$nameLo] = $department['department_name'];
       		//save description
       		$desLo	=	L10nHelper::changeField($lo, "description");
       		$data[$desLo] = $department['department_description'];
       	}
	    
		$this->populateArray($data);
		$this->persist();
		return $this->department_id;
	}

	/**
	 * @author hdviet
	 * update for localize others
	 */
	function getDepartment($departmentId){
		$ds = new Datasource_sql();
		$field	=	L10nHelper::getLocale();
		$nameField	=	L10nHelper::changeField($field, "name");
		$desField	=	L10nHelper::changeField($field, "description");
		
		$sql = array(
			'cols'	=> "department_id, $nameField as name, $desField as description, deleted",
			'from'	=> $this->_table,
			'where'	=> "department_id = '$departmentId'"
		);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) return false;
		else return $result[0];
	}

	public function getDepartmentList(){
		return $this->listAll();
	}
		/**
	 * @author hdviet
	 * update for localize others
	 */

	function getAllDepartments(){
		$field	=	L10nHelper::getLocale();
		$nameField	=	L10nHelper::changeField($field,"name");
		$desField 	=	L10nHelper::changeField($field, "description");

		$ds = new Datasource_sql();
		$sql = array (
			'cols' 		=> "department_id, $nameField as name, $desField as description, t.deleted",
			'from'		=> "department t",
			'orderby'	=> "name");
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
		/**
	 * @author hdviet
	 * update for localize others
	 */
	function updateDepartment($department){
		$field		=	L10nHelper::getLocale();
		$nameField	=	L10nHelper::changeField($field, "name");
		$desField	=	L10nHelper::changeField($field, "description");
	    
	    $data[$nameField] = $department['department_name'];
	    $data[$desField] = $department['department_description'];
	    
        $this->setup($department['department_id']);
        $this->populateArray($data);
        $this->persist();
	}

	function removeOrUndoDepartment($departmentId, $action){
		$sql = "";
		if ($action == 'remove') {
		    $sql = "UPDATE department SET deleted = 1 WHERE department_id = '$departmentId'";
		} else if ($action == 'undo') {
			$sql = "UPDATE department SET deleted = 0 WHERE department_id = '$departmentId'";
		}
		$this->_execute($sql);
//		$sql = "UPDATE room SET department_id=0 WHERE department_id = '$departmentId'";
//		$this->_execute($sql);
	}

}
?>
